package com.test.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundAccountQueryModel;
import com.alipay.api.request.AlipayFundAccountQueryRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayFundAccountQueryResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController
@RequestMapping("/pay")
public class PayController {
    @Value("${pay.alipay.USER2_ID}")
    private String USER1_ID;
    @Value("${pay.alipay.APP2_ID}")
    private String APP1_ID;
    @Value("${pay.alipay.APP2_PRIVATE_KEY}")
    private String APP1_PRIVATE_KEY;
    @Value("${pay.alipay.APP2_PUBLIC_KEY}")
    private String APP1_PUBLIC_KEY;

    /**
     * 电脑网站页面支付(前端点击提交订单或者结算时调用这个接口，返回前端一个form表单，里面有立即支付按钮)  支付API-电脑网站支付-第一个统一收单下单并支付页面接口
     */
    @GetMapping("/pagepay")
    public void pagePay(HttpServletResponse httpResponse) throws IOException, AlipayApiException {
        //创建客户端
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP1_ID, APP1_PRIVATE_KEY, "json", "utf-8", APP1_PUBLIC_KEY, "RSA2");
        //创建对应的request
        AlipayTradePagePayRequest pagePayRequest = new AlipayTradePagePayRequest();
        //设置回调地址
        pagePayRequest.setReturnUrl("http://localhost:8080/page/return");
        //设置通知地址
        pagePayRequest.setNotifyUrl("http://localhost:8080/page/notice");
        //设置业务参数
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "20210817010101004");
        bizContent.put("total_amount", 0.01);
        bizContent.put("subject", "测试商品");
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
        pagePayRequest.setBizContent(bizContent.toString());

        //调用SDK
        AlipayTradePagePayResponse response = alipayClient.pageExecute(pagePayRequest);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        //调用SDK返回的表单
        String form = response.getBody();
        httpResponse.setContentType("text/html;charset=" + "utf-8");
        //直接将完整的表单html输出到页面
        httpResponse.getWriter().write(form);
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }

    /**
     * 电脑网站页面查询 支付API-电脑网站支付-统一收单线下交易查询
     */
    @GetMapping("/pagequery")
    public AlipayTradeQueryResponse pagequery() throws AlipayApiException {
        //创建客户端
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP1_ID, APP1_PRIVATE_KEY, "json", "GBK", APP1_PUBLIC_KEY, "RSA2");
        //创建对应的request
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
//        bizContent.put("out_trade_no", "20150320010101001");
//        bizContent.put("trade_no", "2014112611001004680073956707");
        request.setBizContent(bizContent.toString());

        //调用SDK
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response;
    }

    /**
     * 余额   资金API-支付宝资金账户资产查询接口
     *
     */
    @GetMapping("/amount")
    public String amount() throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        alipayConfig.setAppId(APP1_ID);
        alipayConfig.setPrivateKey(APP1_PRIVATE_KEY);
        alipayConfig.setAlipayPublicKey(APP1_PUBLIC_KEY);
        alipayConfig.setFormat("json");
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayFundAccountQueryRequest request = new AlipayFundAccountQueryRequest();
        AlipayFundAccountQueryModel model = new AlipayFundAccountQueryModel();
        model.setAlipayUserId(USER1_ID);
        model.setAccountType("ACCTRANS_ACCOUNT");
        request.setBizModel(model);
        AlipayFundAccountQueryResponse response = alipayClient.execute(request);
        String availableAmount = response.getAvailableAmount();
        if (response.isSuccess()) {
            System.out.println("调用成功，账号可用余额：" + availableAmount);
            return "账号可用余额：" + availableAmount;
        } else {
            System.out.println("调用失败");
        }
        return null;
    }
}